package com.song.EMS.wx.controller;

import com.song.EMS.bean.Express;
import com.song.EMS.bean.Message;
import com.song.EMS.mvc.ResponseBody;
import com.song.EMS.service.impl.ExpressServiceImpl;
import com.song.EMS.utils.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ExpressController {
    private ExpressServiceImpl expressService = new ExpressServiceImpl();
    /**
     * 快递员添加快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/expressAdd.do")
    public String expressInsert(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String expressNumber = request.getParameter("expressNumber");
        String receiverName = request.getParameter("receiverName");
        String receiverPhone = request.getParameter("receiverPhone");
        String company = request.getParameter("company");

        String loginUser = UserUtils.getUserPhone(request);
        Express express = new Express(expressNumber, company, receiverName, receiverPhone, loginUser);
        //调用快递的service
        Integer integer = expressService.insertExpress(express);
        if (integer == 1) {
            data = new Message(1, "添加成功");
        } else if (integer == -1) {
            data = new Message(-1, "快递单号已存在");
        } else {
            data = new Message(0, "添加失败");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过手机号查询未取快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/searchExpressByPhone.do")
    public String selectExpress(HttpServletRequest request,HttpServletResponse response){
        Message data = null;
        String userPhone = request.getParameter("userPhone");


        List<Express> expresses = expressService.selectExpress(userPhone, 1);
        if (expresses.size() >0){
            data = new Message(1,null,expresses);
        }else {
            data = new Message(0,"没有查询到快递");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过验证码查询快递信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/searchExpressByCode.do")
    public String selectExpressByCode(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String code = request.getParameter("code");
        System.out.println(code);
        if(code != null&& !code.equals("")){
            Express express = expressService.selectByCode(code);
            System.out.println(express);
            if (express != null){
                data = new Message(1,null,express);
            }else {
                data = new Message(0, "没有快递信息");
            }
        }else{
            data = new Message(-1, "取件码不能为空");
        }

        return JsonUtil.toJSON(data);
    }

    /**
     * 确认收获，通过code更新快递状态
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/updateStatus.do")
    public String updateStatus(HttpServletRequest request, HttpServletResponse response) {
        Message data = null;
        String code = request.getParameter("code");
        Boolean aBoolean = expressService.updateStatus(code);
        if (aBoolean){
            data = new Message(1,"确认收货");
        }else {
            data = new Message(0, "收货失败");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过快递单号查询快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/selectByNumber.do")
    public String selectByNumber(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String expressNumber = request.getParameter("expressNumber");
        //调用service
        Express express = expressService.selectByNumber(expressNumber);
        if (express == null){
            data = new Message(0,"没有快递信息");
        }else{
             request.getSession().setAttribute("express",express);
            data = new Message(1,null,express);
        }
        System.out.println("------" + express);
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过快递单号查询到的快递信息de加载
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/expressListByNum.do")
    public String getExpressByNumber(HttpServletRequest request, HttpServletResponse response){
        Express express = (Express) request.getSession().getAttribute("express");
        Message data = new Message(0,"查询成功",express);
        System.out.println("======="+express);
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过快递员手机号查询所有其处理过的快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/infoExpress.do")
    public String getExpressInfo(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取此时登录快递员的手机号
        String loginUserPhone = UserUtils.getUserPhone(request);
        System.out.println(loginUserPhone);
        //调用service
        List<Express> expresses = expressService.selectExpressByWriterPhone(loginUserPhone, 0);
        if (expresses.size() >0){
            data = new Message(1,null,expresses);
        }else {
            data = new Message(0,"没有查询到快递");
        }
        return JsonUtil.toJSON(data);
    }

}
